package com.dimples.dd.admin.core.runner;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/5/22
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class StartedUpRunner implements ApplicationRunner {

    private final ConfigurableApplicationContext context;
    private final Environment environment;

    @Override
    public void run(ApplicationArguments args) {
        if (context.isActive()) {
            ThreadUtil.execute(() -> {
                ThreadUtil.sleep(1, TimeUnit.SECONDS); // 延迟 1 秒，保证输出到结尾
                String port = StrUtil.toStringOrNull(environment.getProperty("server.port"));
                String user = StrUtil.toStringOrNull(environment.getProperty("spring.security.user.name"));
                String pass = StrUtil.toStringOrNull(environment.getProperty("spring.security.user.password"));
                System.out.println(StrUtil.format("""

                        ----------------------------------------------------------
                        \t启动地址: \t{}\s
                        \t项目启动成功！
                        \t用户登录信息: \t user:【{}】\t pass:【{}】\s
                        ----------------------------------------------------------""",
                        StrUtil.format("http://localhost:{}/", port),
                        user,
                        pass));
            });
        }
    }
}
